//
//  SelectionViewCotroller.m
//  ProjectA_ManualGuest
//
//  Created by dingcong on 16/6/14.
//  Copyright © 2016年 丁聪. All rights reserved.
//

#import "SelectionViewCotroller.h"
#import "CellOfSelection.h"
#import "NetWorkingHandle.h"
#import "SelectionModel.h"
#import "CellOfDoubleImage.h"
#import "CellOfOnePic.h"
#import "CellOfLine.h"
#import "RootVCOfVidio.h"
#import "RootVCOfTutorial.h"
#import "UIImageView+WebCache.h"
#import <WebKit/WebKit.h>
#import "VcOfWebView.h"
#import <MJRefresh.h>
#import "Masonry.h"
#import <AFNetworking.h>
#import "VCOfLive.h"
#import "DBHandler.h"
#import "SandBoxStore.h"
#import "MD5Handler.h"
#import "Settings.h"
@interface SelectionViewCotroller ()<UIScrollViewDelegate>

@property(nonatomic, retain)NSMutableArray *mArr;

@property(nonatomic, retain)NSMutableDictionary *mDicOfData;

@property(nonatomic, retain)NSMutableArray *arrOfSecond;

@property(nonatomic, retain)NSMutableArray *arrOfThird;

@property(nonatomic, retain)NSMutableArray *arrOfFourth;

@property(nonatomic, retain)NSMutableArray *arrOfFirst;

@property(nonatomic, retain)UIScrollView *scroll;

@property(nonatomic, retain)NSString *strOftopic_id;

@property(nonatomic, retain)NSString *strOftopic_type;

@property(nonatomic, retain)WKWebView *wk;

@property (nonatomic, strong) NSMutableArray * articles;

@property (assign, nonatomic) NSInteger page;

@end

@implementation SelectionViewCotroller


- (NSMutableArray *)articles
{
    if (nil == _articles) {
        _articles = [NSMutableArray arrayWithCapacity: 42];
    }
    
    return _articles;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    
    // 马上进入刷新状态
    
}

- (void)dealloc
{
    
    [super dealloc];
    [_strOflink_info release];
    [_mArr release];
    [_mDicOfData release];
    [_arrOfSecond release];
    [_arrOfThird release];
    [_arrOfFourth release];
    [_arrOfFirst release];
    [_scroll release];
    [_articles release];
    [_wk release];
    [_strOftopic_id release];
    [_strOftopic_type release];
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self ToGetData];
    
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        
        self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.tabBarController.tabBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
        self.navigationController.navigationBar.barTintColor = [[Settings shareSettings]color:SkyBlurToGray];
       
    }];
    [self config];
    
}

- (void)config
{
    
    [self.tableView registerClass:[CellOfSelection class] forCellReuseIdentifier:@"pool1"];
    [self.tableView registerClass:[CellOfDoubleImage class] forCellReuseIdentifier:@"pool2"];
    [self.tableView registerClass:[CellOfOnePic class] forCellReuseIdentifier:@"pool3"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CellOfLine class] forCellReuseIdentifier:@"pool4"];
    [self.tableView.mj_header setLastUpdatedTimeKey:@"time"];
    
    
    
    
#pragma mark - 头部下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        
        [self ToGetData];
        [self.tableView.mj_header endRefreshing];
        
        
        
    }];
    
    
   
//    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
//    
//        [header setTitle:@"松开 老衲刷新!!!😄" forState:MJRefreshStateIdle];
//    
//        [header beginRefreshing];
//    
//    self.tableView.mj_header = header;

    
    [self.tableView.mj_header beginRefreshing];
    [self creatHeaderView];

    
    
    
    
    
}

- (void)loadNewData
{
    
    [self.tableView reloadData];
    
    
    
}

#pragma mark - 创建TableView头部view(滚动视图)
- (void)creatHeaderView
{
    
    UIView *viewHead = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    self.tabBarController.tabBar.tintColor = [UIColor colorWithRed:0.0 / 256.0f  green:208.0 / 256.0f blue:252.0 / 256.0f alpha:1];
    
//    viewHead.backgroundColor = [UIColor redColor];
    
    self.tableView.tableHeaderView = viewHead;
    [viewHead release];
    [self.tableView.tableHeaderView release];
    
    
    
    
    
}

- (void)creatScrollOfHeader
{
    self.scroll = [[UIScrollView alloc]initWithFrame:self.tableView.tableHeaderView.bounds];
    
    
    
    [self.tableView.tableHeaderView addSubview: self.scroll];
    
    self.scroll.contentSize = CGSizeMake(SCREEN_WIDTH * self.arrOfFirst.count, CGRectGetHeight(self.scroll.bounds));
    
    self.scroll.pagingEnabled = YES;
    
#pragma mark - ScrollView添加图片
    
    
    NSMutableArray *urlarr = [NSMutableArray array];
    for (int i = 0; i < self.arrOfFirst.count; i++)
    {
       NSString *str = [[self.arrOfFirst objectAtIndex:i]objectForKey:@"host_pic"];
       
        [urlarr addObject:str];
        UIImageView *imageOfScroll = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, CGRectGetHeight(self.scroll.bounds))];
        [imageOfScroll sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
        [self.scroll addSubview:imageOfScroll];
        [imageOfScroll release];
        
    }
    
    
    
}

#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 100;
    }
    else if(indexPath.row == 1)
    {
        
        return 160;
    }
    else if(indexPath.row == 2)
    {
        
        return 40;
    }
    else{
        
        
        return 180;
    }
    
    
}

#pragma mark - 行数 dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.arrOfFourth.count + 3;
}

#pragma mark - Cell类型 dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    if (indexPath.row == 0)
    {
//        NSLog(@"测试数组:%@", self.arrOfSecond);
        
        CellOfSelection *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        [cell.tap addTarget:self action:@selector(pushVCOfVidio:)];
        
        [cell.webViewTap addTarget:self action:@selector(pushWebViewOfSpecialSubject:)];
        
        
        [cell.tapOfLive addTarget:self action:@selector(pushvcOfLive:)];
        for (NSInteger i = 0; i < self.arrOfSecond.count; i++)
        {
            cell.model = self.arrOfSecond[i];

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        
        return cell;
    }
    else if (indexPath.row == 1)
    {
        CellOfDoubleImage *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        [cell.tap addTarget:self action:@selector(JumpVC:)];
        [cell.tap2 addTarget:self action:@selector(JumpVCOfTopic:)];
        for (int i = 0; i < self.arrOfThird.count; i++)
        {
            cell.model = self.arrOfThird[i];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    else if(indexPath.row == 2)
    {
//        NSLog(@"测试数组2:%@", self.arrOfThird);
        
        
        
        CellOfLine *cell = [tableView dequeueReusableCellWithIdentifier:@"pool4"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
     
        return cell;
        
        
    }
    
    else
    {
        CellOfOnePic *cell = [tableView dequeueReusableCellWithIdentifier:@"pool3"];
        
        cell.model = self.arrOfFourth[indexPath.row - 3];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return cell;
        
        
    }
    
    
    
}

- (void)pushvcOfLive:(UITapGestureRecognizer *)pushVC
{
    

    VCOfLive *vc = [[VCOfLive alloc]init];
    
    
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
    
    
}

#pragma mark - imageView点击事件
- (void)pushVCOfVidio:(UITapGestureRecognizer *)pushvidio
{
    
//    RootVCOfVidio *vidio = [[RootVCOfVidio alloc]init];
//    self.tabBarController.selectedIndex = 1;
    
    self.tabBarController.selectedViewController = self.tabBarController.viewControllers[1];
    
//    UICollectionViewController *cc = self.tabBarController.viewControllers[1].childViewControllers[0];
//    cc.collectionView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
//    self.tabBarController.selectedViewController = vidio;
//    self.tabBarController.selectedViewController = vidio;
    
//    [vidio release];
    
    
    
}

- (void)JumpVC:(UITapGestureRecognizer *)jump
{
    

    self.tabBarController.selectedViewController = self.tabBarController.viewControllers[1];
    
    
}

- (void)JumpVCOfTopic:(UITapGestureRecognizer *)jump
{
    
    
    self.tabBarController.selectedViewController = self.tabBarController.viewControllers[2];
    
    
}

#pragma mark - tableView Cell Push 二级界面=
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    
    if (indexPath.row >= 3)
    {
        
        SelectionModel *model = [_arrOfFourth objectAtIndex:indexPath.row -3];
        self.strOftopic_id = model.mob_h5_url;
        NSLog(@"%@", self.strOftopic_id);
        
        VcOfWebView *webVc = [[VcOfWebView alloc]init];
        webVc.strOfId = model.mob_h5_url;
        webVc.strOfname = model.subject;
        
        webVc.model = [[NewModel alloc]init];
        
        webVc.model.subject = model.subject;
        webVc.model.pic = model.pic;
        webVc.model.mob_h5_url = model.mob_h5_url;
        

        
        [self.navigationController pushViewController:webVc animated:YES];
        
        [webVc.model release];
        [webVc release];
    }
    
    

    
    
}

- (void)pushWebViewOfSpecialSubject:(UITapGestureRecognizer *)subject
{
    
    SelectionModel *model = [_arrOfSecond objectAtIndex:2];
                             
    NSLog(@"%@", model.link_info);
    self.strOflink_info = model.link_info;
                             
    NSString *url = [NSString stringWithFormat:@"http://www.shougongke.com/index.php?m=Topic&a=topicDetail&topic_id=%@&topic_type=shiji", self.strOflink_info];
    NSLog(@"%@", url);
    
    
    VcOfWebView *webVc = [[VcOfWebView alloc]init];
    
    
    self.wk = [[WKWebView alloc]init];
    [self.wk loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    self.wk.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49);

    
    
    [webVc.view addSubview:self.wk];
   [self.navigationController pushViewController:webVc animated:YES];
    [self.wk release];
    [webVc release];
    
    
    
}

#pragma mark - 数据
- (void)ToGetData
{
    self.mDicOfData = [NSMutableDictionary dictionary];
    self.arrOfSecond = [NSMutableArray array];
    self.arrOfThird = [NSMutableArray array];
    self.arrOfFourth = [NSMutableArray array];
    self.arrOfFirst = [NSMutableArray array];
    NSString *url = @"http://m.shougongke.com/index.php?&c=index&a=indexNewest&vid=18";
//                    @"http://m.shougongke.com/index.php?&c=index&a=indexNewest&vid=18"
    NetWorkingHandle *net = [[NetWorkingHandle alloc]init];
    
    NSString *strMD5 = [MD5Handler stringFromMD5:url];
    
    SandBoxStore *sand = [SandBoxStore sharedSandBoxStore];
    //反归档取数据
   id results = [sand readByUnArchiverWithKey:strMD5];
    if (!results) {
        
        [net netWorkingHandlerGetWithURL:url completion:^(id result, NSData *data, NSURLResponse *response, NSError *error)
         {
             //数据
             self.mDicOfData = [result objectForKey:@"data"];
             
             self.arrOfFirst = [self.mDicOfData objectForKey:@"slide"];
             
             [self creatScrollOfHeader];
             
             NSArray *arr = [self.mDicOfData objectForKey:@"navigation"];
             
             for (NSDictionary *dic in arr)
             {
                 SelectionModel *model = [[SelectionModel alloc]init];
                 [model setValuesForKeysWithDictionary:dic];
                 [self.arrOfSecond addObject:model];
                 
                 [model release];
             }
             
             
             NSArray *arr2 = [self.mDicOfData objectForKey:@"advance"];
             
             for (NSDictionary *dic in arr2)
             {
                 SelectionModel *model = [[SelectionModel alloc]init];
                 [model setValuesForKeysWithDictionary:dic];
                 [self.arrOfThird addObject:model];
                 
                 [model release];
             }
             
             
             NSArray *arr3 = [self.mDicOfData objectForKey:@"hotTopic"];
             for (NSDictionary *dic in arr3)
             {
                 SelectionModel *model = [[SelectionModel alloc]init];
                 [model setValuesForKeysWithDictionary:dic];
                 [self.arrOfFourth addObject:model];
                 //            NSLog(@"arrOfFourth:%@", self.arrOfFourth);
                 [model release];
             }
             
             
             [self.tableView reloadData];
             
             
             
             
             
         }];
    }
    else
    {
        
        self.mDicOfData = [results objectForKey:@"data"];
        //        NSLog(@"测试数据:%@", self.mDic);
        
        self.arrOfFirst = [self.mDicOfData objectForKey:@"slide"];
        
        //            for (NSDictionary *dic in arr4)
        //            {
        //                SelectionModel *model = [[SelectionModel alloc]init];
        //                [model setValuesForKeysWithDictionary:dic];
        //                [self.arrOfFirst addObject:model];
        //
        //                [model release];
        //
        //            }
        //            NSLog(@"%ld", self.arrOfFirst.count);
        
        [self creatScrollOfHeader];
        
        NSArray *arr = [self.mDicOfData objectForKey:@"navigation"];
        //        NSLog(@"第二数组:%@", arr);
        for (NSDictionary *dic in arr)
        {
            SelectionModel *model = [[SelectionModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrOfSecond addObject:model];
            //            NSLog(@"arrOfSecond:%@", self.arrOfSecond);
            [model release];
        }
        
        
        NSArray *arr2 = [self.mDicOfData objectForKey:@"advance"];
        
        for (NSDictionary *dic in arr2)
        {
            SelectionModel *model = [[SelectionModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrOfThird addObject:model];
            //            NSLog(@"arrOfThird:%@", self.arrOfThird);
            [model release];
        }
        
        
        NSArray *arr3 = [self.mDicOfData objectForKey:@"hotTopic"];
        for (NSDictionary *dic in arr3)
        {
            SelectionModel *model = [[SelectionModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrOfFourth addObject:model];
            //            NSLog(@"arrOfFourth:%@", self.arrOfFourth);
            [model release];
        }
        
        
        [self.tableView reloadData];
    }
    
    
    
    [net release];
    
}




@end
